package com.jt.serviceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.exception.ServiceException;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.service.DubboUserService;
import com.jt.utils.Assert;
import com.jt.utils.ObjectMapperUtil;
import com.jt.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import redis.clients.jedis.JedisCluster;

import java.util.Date;
import java.util.UUID;

/**
 * 用户服务提供者
 * 实现dubbo 用户服务接口
 * @author dc
 * @Classname DubboUserServiceImpl
 * @Date 2020/2/26 11:56
 */
@Service
public class DubboUserServiceImpl implements DubboUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JedisCluster redis;

    @Override
    public void saveUser(UserVO userVO) {
        Assert.isValid(userVO==null,"请先填写用户信息!");
        User user=new User();
        String in_mobileCode = userVO.getMobileCode();
        String mobileCode = redis.get("MobileCode");
        if(!mobileCode.isEmpty()){
            if(mobileCode.equals(in_mobileCode)){
                user.setPassword(userVO.getPassword());
                String md5Password = DigestUtils.md5DigestAsHex(userVO.getPassword().getBytes());
                user.setPassword(md5Password).setEmail(userVO.getPhone()).setUsername(userVO.getUsername()).setPhone(userVO.getPhone()).setCreated(new Date()).setUpdated(user.getCreated());
                int rows = userMapper.insert(user);
                if(rows<1){
                    throw new ServiceException("注册失败,请联系管理员!");
                }
            }else{
                throw new ServiceException("验证码错误，请重新注册");
            }
        }else{
            throw new ServiceException("验证码不能为空，请重新注册");
        }

    }

    @Override
    public String findUserByUP(User user) {
        //注册和登录的密码加密必须一致
        String md5Password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(md5Password);
        //根据对象中不为null的属性充当where条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<>(user);
        User userDB = userMapper.selectOne(queryWrapper);
        if(userDB==null){
            //用户名或者密码错误
            return null;
        }
        //表示用户信息有效,需要进行单点登录操作
        String ticket = UUID.randomUUID().toString();
        //脱敏处理,伪造数据user:username,password,idCard...
        userDB.setPassword("123456");
        String userJson = ObjectMapperUtil.toJson(userDB);
        redis.setex(ticket,7*24*3600,userJson);
        return ticket;
    }

    @Override
    public void updateByPwd(UserVO userVO) {
        Assert.isValid(userVO==null,"请先填写用户信息!");
        if ("1".equals(userVO.getOpt())){
            //忘记密码
            String in_mobileCode = userVO.getMobileCode();
            String mobileCode = redis.get("MobileCode");
            if(!mobileCode.isEmpty()) {
                QueryWrapper<User> query = new QueryWrapper<>();
                query.eq("username", userVO.getUsername())
                        .eq("phone", userVO.getPhone());
                User user = userMapper.selectOne(query);
                if (user!=null) {
                    if (mobileCode.equals(in_mobileCode)) {
                        String md5Password = DigestUtils.md5DigestAsHex(userVO.getPassword().getBytes());
                        user.setPassword(md5Password).setUpdated(new Date());
                        int rows = userMapper.updateById(user);
                        if (rows < 1) {
                            throw new ServiceException("修改失败,请联系管理员!");
                        }
                    } else {
                        throw new ServiceException("验证码错误，请重新输入");
                    }
                }else{
                    throw new ServiceException("该用户不存在或手机号不正确！");
                }
            }else {
                throw new ServiceException("验证码为空，请输入验证码");
            }
        }else if ("2".equals(userVO.getOpt())){
            //修改密码
            String md5OldPwd = DigestUtils.md5DigestAsHex(userVO.getOldPwd().getBytes());
            QueryWrapper<User> query = new QueryWrapper<>();
            query.eq("id", userVO.getId())
                    .eq("password", md5OldPwd);
            User user = userMapper.selectOne(query);
            if (user!=null){
                String md5Password = DigestUtils.md5DigestAsHex(userVO.getPassword().getBytes());
                user.setPassword(md5Password).setUpdated(new Date());
                int rows = userMapper.updateById(user);
                if (rows < 1) {
                    throw new ServiceException("修改失败,请联系管理员!");
                }
            }else {
                throw new ServiceException("旧密码错误");
            }
        }else{
            throw new ServiceException("密码操作失败");
        }
    }
}
